package com.xique.blelock.view.DeviceManager;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleDevice;
import com.xique.blelock.Interface.BleConnectListen;
import com.xique.blelock.R;
import com.xique.blelock.Utils.BSSIDUtil;
import com.xique.blelock.Utils.BleUtils;
import com.xique.blelock.Utils.Stringkey;
import com.xique.blelock.model.LockInfo;
import com.xique.blelock.view.BaseActivity;
import com.xique.blelock.view.Home_Activity;
import com.xique.blelock.view.Thtreepackage.CircleWaveView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/12/25.
 */

public class ConnectAnimationActivity extends BaseActivity implements BleConnectListen {


    @BindView(R.id.connectanimation)
    CircleWaveView circleWaveView;

    @BindView(R.id.conneting)
    TextView connecting;

    @BindView(R.id.connecresult)
    TextView connecsucceview;

    @BindView(R.id.connectfaillayout)
    View connectfailed;

    @BindView(R.id.connecttinglayout)
    View connecttinglayout;


    @BindView(R.id.title)
    TextView titile;

    //传递进来的设备
    private LockInfo lockInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connectlayout);
        ButterKnife.bind(this);
        initView();
    }

    @OnClick(R.id.BACKER)
    public void BackLastPage(){
        finish();
    }


    @Override
    protected void initView() {
        super.initView();

        lockInfo = (LockInfo) getIntent().getSerializableExtra(Stringkey.BLEDevice);
        circleWaveView.setWaveColor(getResources().getColor(R.color.coloblueAY));

        BleUtils.setConnectListen(this);
        BleUtils.connectDev(BSSIDUtil.restoreBSSID(lockInfo.getLockId()).toUpperCase(), this);

        titile.setText(lockInfo.getLockName());
    }

    @OnClick(R.id.reconnect)
    public void ReconnectBle(){
        if(BleManager.getInstance().isConnected(BleUtils.getBleDevice())){
            showToast(this, "已经连接");
        }else{
            connecttinglayout.setVisibility(View.VISIBLE);
            connectfailed.setVisibility(View.GONE);
            BleUtils.connectDev(BSSIDUtil.restoreBSSID(lockInfo.getLockId()).toUpperCase(), this);
        }
    }


    private void jumpControllerPage(BleDevice bleDevice) {
        Intent intent = new Intent(this, DeviceControllerActivity.class);
        intent.putExtra(Stringkey.DeviceInfo, bleDevice);
        startActivity(intent);
        finish();

    }

    @Override
    public void didConnetced(final  BleDevice bleDevice) {
        connecting.setVisibility(View.GONE);
        connecsucceview.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                jumpControllerPage(bleDevice);
            }
        }, 200);

    }

    @Override
    public void didDisConnect(BleDevice bleDevice) {
//        connecting.setText("连接失败");
        connecttinglayout.setVisibility(View.GONE);
        connectfailed.setVisibility(View.VISIBLE);

    }

}
